'use strict';

app.controller('changePswCtl', function($scope,$http,rest,$compile,$location,ngDialog,user,$state,$window,uiLoad,$rootScope,$ocLazyLoad,tipService) {
	$scope.loginName = $window.localStorage.getItem("loginName");
	var token = $window.localStorage.getItem("token");
	//初始化数据
	$scope.oldPassword = "";
	$scope.newPassword = "";
	$scope.comfirmedPassword = "";
	
	//保存修改密码
	$scope.saveData = function(){
		var btn =  true;
		//非空 、长度、是否相同校验
		if($scope.oldPassword == ""){
        	layer.tips('请输入原密码', '.oldPassword-Tips',{
			  tipsMore: true,
			  tips: [2,'#ed5565']
			});
    		btn =  false;
        } else if($scope.oldPassword.length < 6){
        	layer.tips('密码长度不能小6位数', '.oldPassword-Tips',{
			  tipsMore: true,
			  tips: [2,'#ed5565']
			});
    		btn =  false;
        };
        if($scope.newPassword == ""){
        	layer.tips('请输入新密码', '.newPassword-Tips',{
			  tipsMore: true,
			  tips: [2,'#ed5565']
			});
    		btn =  false;
        } else if($scope.newPassword.length < 6){
        	layer.tips('密码长度不能小6位数', '.newPassword-Tips',{
			  tipsMore: true,
			  tips: [2,'#ed5565']
			});
    		btn =  false;
        };
        if($scope.comfirmedPassword == ""){
        	layer.tips('请再次输入新密码', '.comfirmedPassword-Tips',{
			  tipsMore: true,
			  tips: [2,'#ed5565']
			});
    		btn =  false;
        } else if($scope.comfirmedPassword.length < 6){
        	layer.tips('密码长度不能小6位数', '.comfirmedPassword-Tips',{
			  tipsMore: true,
			  tips: [2,'#ed5565']
			});
    		btn =  false;
        } else if($scope.comfirmedPassword != $scope.newPassword){
        	layer.tips('两次密码不相同', '.comfirmedPassword-Tips',{
			  tipsMore: true,
			  tips: [2,'#ed5565']
			});
    		btn =  false;
        }
        
        if(btn){
        	var formData = {
        		pwd: $scope.oldPassword,
        		newPwd: $scope.newPassword,
        		repeatPwd: $scope.comfirmedPassword
        	}
        	rest.decoJava({
        		type: 'post',
        		url: '/tenancy-sys/admin/user/update/password',
        		data: formData,
        		beforeSend: function(request){
					request.setRequestHeader("token", token); 
				},
        		success: function(res){
        			console.log(res);
        			if ("0000" == res.code) {
						layer.alert(res.message, {
							closeBtn: 0,
				        	offset:'t',
				        	icon: 1,
				        	btn: ['确定'],
				        	time: 0, //不自动关闭
							yes: function(index){
								layer.close(index);
								$state.go('access.signin');
						   }
						});
                    } else {
                    	layer.msg(res.message,{
		    				offset:'t'
		    			});
                    }
        		}
        	});
        } 
	}
});